package leetcode.Hot100;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Cheng Jun
 * Description: 给定一个仅包含数字 2-9 的字符串，返回所有它能表示的字母组合。答案可以按 任意顺序 返回。
 * <p>
 * 给出数字到字母的映射如下（与电话按键相同）。注意 1 不对应任何字母。
 * <p>
 * 来源：力扣（LeetCode）
 * 链接：https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number
 * 著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
 * @version 1.0
 * @date 2021/11/30 20:42
 * 二刷
 */
public class letterCombinations {
    static HashMap<String, String[]> letterMap = new HashMap<>(16);

    public static void main(String[] args) {
        System.out.println(letterCombinations(""));
    }

    static List<String> letterCombinations(String digits) {

        List<String> resultList = new ArrayList<>();
        if (digits.length() == 0) {
            return resultList;
        }
        letterMap.put("2", new String[]{"a", "b", "c"});
        letterMap.put("3", new String[]{"d", "e", "f"});
        letterMap.put("4", new String[]{"g", "h", "i"});
        letterMap.put("5", new String[]{"j", "k", "l"});
        letterMap.put("6", new String[]{"m", "n", "o"});
        letterMap.put("7", new String[]{"p", "q", "r", "s"});
        letterMap.put("8", new String[]{"t", "u", "v"});
        letterMap.put("9", new String[]{"w", "x", "y", "z"});
        String[] split = digits.split("");
        for (int i = 0; i < split.length; i++) {
            combinate(resultList, letterMap.get(split[i]));
        }
        return resultList;
    }

    static List<String> combinate(List<String> resultList, String[] letter) {
        if (resultList.isEmpty()) {
            resultList.addAll(Arrays.stream(letter).collect(Collectors.toList()));
        } else {
            ArrayList<String> list = new ArrayList<>();
            for (String s : resultList) {
                for (String j : letter) {
                    list.add(s + j);
                }
            }
            resultList.clear();
            resultList.addAll(list);
        }
        return resultList;
    }
}
